package piis;

import Algorithms.SimulatedAnnealing;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import org.jfree.ui.RefineryUtilities;
import utilities.PiisWriter;

/**
 * Main class for the second part of the first assignment, 
 * which is implementation of Simulated Annealing.
 * 
 * @author Atuan
 */
public class Piis {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        SimulatedAnnealing sa = new SimulatedAnnealing();
//        double min = -5;
//        double max = 6;
        double min = -2;
        double max = 5;
//        double min = -6;
//        double max = 4;
        SimulatedAnnealingSolution sas = sa.findMin(700000,min,max);
        System.out.println("X = "+sas.getBestX() + " - Y = "+sas.getBestResult());
        
        // Preparation of data for the plot.
        Map<Double,Double> data = new HashMap<>();
        for(double i = min; i <= max; i+=0.01){
//        for(double i = -1.5; i <= 2; i+=0.01){
            data.put(Double.valueOf(i), sa.func(i));
        }
        data.put(sas.getBestX(),sas.getBestResult());
        
        // Create a plot.
        Plot demo = new Plot("Assignment 1","Function",data,sas.getBestX());
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.setVisible(true);
        System.out.println("It's been: "+(System.currentTimeMillis()-time)/1000.0+" seconds.");
        
        PiisWriter.write(sa.printResult, "sa_2_result.out");
        System.out.println("Writing complete");
    }
    
    
}
